package com.codeoss.framework.webide.app.service.impl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.converter.IConverter;
import com.codeoss.framework.converter.IConverterStore;
import com.codeoss.framework.data.metadata.Query;
import com.codeoss.framework.data.metadata.Restrictions;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.impexp.ExportManager;
import com.codeoss.framework.webide.app.service.CommonAppDataHandler;
import com.codeoss.module.ir.model.IrApplication;
import com.codeoss.module.ir.model.IrModel;
import com.codeoss.module.ir.model.IrModelAccess;
import com.codeoss.module.ir.service.IIrModelAccessService;
import com.codeoss.module.ir.service.IIrModelService;
import com.codeoss.module.res.model.ResRole;
import com.codeoss.module.res.service.IResRoleService;
@Service("ir_model_accessAppDataHandler")
public class IrModelAccessAppDataHandler extends CommonAppDataHandler {
    @Autowired
    private IIrModelAccessService irModelAccessService;
    @Autowired
    private IIrModelService irModelService;
    @Autowired
    private IResRoleService resRoleService;
    @Autowired
    private ExportManager exportManager;
    @Autowired
    private ITableStore tableStore;
    @Autowired
    private IConverterStore converterStore;
    
	@Override
	public void dataExport(YunContext yunContext, IrApplication irApplication, String tableName) throws IOException{
		// TODO Auto-generated method stub
		QueryWrapper<IrModel> modelQueryWrapper=new QueryWrapper<IrModel>();
		modelQueryWrapper.in("module_type", getModuleTypeList(yunContext, irApplication));
		List<IrModel> irModelList=irModelService.list(modelQueryWrapper);
		
		QueryWrapper<ResRole> roleQueryWrapper=new QueryWrapper<ResRole>();
		roleQueryWrapper.in("module_type", getModuleTypeList(yunContext, irApplication));
		List<ResRole> resRoleList=resRoleService.list(roleQueryWrapper);
		
		int index=0;
		for(int i=0;i<irModelList.size();i++) {
			IrModel irModel=irModelList.get(i);
			for(int j=0;j<resRoleList.size();j++) {
				ResRole resRole=resRoleList.get(j);
				
				QueryWrapper<IrModelAccess> queryWrapper=new QueryWrapper<IrModelAccess>();
				queryWrapper.eq("ir_model_id", irModel.getId());
				queryWrapper.eq("res_role_id", resRole.getId());
				
				IrModelAccess irModelAccess=irModelAccessService.getOne(queryWrapper);
				if(irModelAccess!=null) {
					Table table=tableStore.get("ir_model_access");
					Query query=new Query();
					query.add(Restrictions.eq("ir_model_id", irModel.getId()));
					query.add(Restrictions.eq("res_role_id", resRole.getId()));
					String filename="ir_model_access"+index;
					writeConfigKeyValue(yunContext, "ir_model_access", filename);
					FileOutputStream os=createFileOutputStream(yunContext, filename);
					exportManager.exportProperties(yunContext, table, query, os);
					os.close();
					index++;
				}
			}
		}
	}

	@Override
	@Transactional
	public void dataImport(YunContext yunContext, IrApplication irApplication, String tableName, List<String> filenameList)throws IOException {
		// TODO Auto-generated method stub
		if(filenameList==null) {
			return;
		}
		for(int i=0;i<filenameList.size();i++) {
			String filename=filenameList.get(i);
			InputStream is=createFileInputStream(yunContext, filename);
			Properties p = new Properties();
			p.load(is);
            is.close();
            
            String sirModelId=p.getProperty("ir_model_id");
            String sresRoleId=p.getProperty("res_role_id");
            String permSelect=p.getProperty("perm_select");
            String permInsert=p.getProperty("perm_insert");
            String permUpdate=p.getProperty("perm_update");
            String permDelete=p.getProperty("perm_delete");
            
            QueryWrapper<ResRole> roleQuery=new QueryWrapper<ResRole>();
            roleQuery.eq("code", sresRoleId);
            ResRole resRole=resRoleService.getOne(roleQuery);
            
            IrModel irModel= irModelService.getIrModelByTableName(sirModelId);
            
            IConverter converter=converterStore.getConverter("boolean");
            
            QueryWrapper<IrModelAccess> queryWrapper=new QueryWrapper<IrModelAccess>();
			queryWrapper.eq("ir_model_id", irModel.getId());
			queryWrapper.eq("res_role_id", resRole.getId());
			IrModelAccess irModelAccess=irModelAccessService.getOne(queryWrapper);
			if(irModelAccess==null) {
				irModelAccess=new IrModelAccess();
			}
			irModelAccess.setIrModelId(irModel.getId());
			irModelAccess.setResRoleId(resRole.getId());
			irModelAccess.setPermSelect((Boolean)converter.convertStringToObject(permSelect));
			irModelAccess.setPermInsert((Boolean)converter.convertStringToObject(permInsert));
			irModelAccess.setPermUpdate((Boolean)converter.convertStringToObject(permUpdate));
			irModelAccess.setPermDelete((Boolean)converter.convertStringToObject(permDelete));
			if(irModelAccess.getId()==null) {
				irModelAccessService.insert(irModelAccess);
			}else {
				irModelAccessService.update(irModelAccess);
			}
		}
	}

}
